home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / OnboardGtk.py < prev    next >
Text File  |  2009-10-01  |  5KB  |  169 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. ### Logging ###
  4. import logging
  5. _logger = logging.getLogger("OnboardGtk")
  6. ###############
  7.  
  8. import sys
  9. import gobject
  10. gobject.threads_init()
  11.  
  12. import gtk
  13. import virtkey
  14. import gettext
  15. import os.path
  16.  
  17. from gettext import gettext as _
  18.  
  19. from Onboard.Keyboard import Keyboard
  20. from Onboard.KeyGtk import *
  21. from Onboard.Pane import Pane
  22. from Onboard.KbdWindow import KbdWindow
  23. from Onboard.KeyboardSVG import KeyboardSVG
  24.  
  25.  
  26. ### Config Singleton ###
  27. from Onboard.Config import Config
  28. config = Config()
  29. ########################
  30.  
  31. import Onboard.KeyCommon
  32.  
  33. # can't we just import Onboard.utils and then use Onboard.utils.run_script ?
  34. from Onboard.utils import run_script
  35.  
  36. import Onboard.utils as utils
  37.  
  38. #setup gettext
  39. app="onboard"
  40. gettext.textdomain(app)
  41. gettext.bindtextdomain(app)
  42.  
  43. DEFAULT_FONTSIZE = 10
  44.  
  45. class OnboardGtk(object):
  46.     """
  47.     This class is a mishmash of things that I didn't have time to refactor in to seperate classes.
  48.     It needs a lot of work.
  49.     The name comes from onboards original working name of simple onscreen keyboard.
  50.     """
  51.     
  52.     """ Window holding the keyboard widget """
  53.     _window = KbdWindow()
  54.  
  55.     def __init__(self, main=True):
  56.         sys.path.append(os.path.join(config.install_dir, 'scripts'))
  57.  
  58.         # this object is the source of all layout info and where we send key presses to be emulated.
  59.  
  60.         _logger.info("Getting user settings")
  61.  
  62.         self.load_layout(config.layout_filename)
  63.         config.layout_filename_notify_add(self.load_layout)
  64.  
  65.         _logger.info("Creating trayicon")
  66.         #Create menu for trayicon
  67.         uiManager = gtk.UIManager()
  68.  
  69.         actionGroup = gtk.ActionGroup('UIManagerExample')
  70.         actionGroup.add_actions([('Quit', gtk.STOCK_QUIT, _('_Quit'), None,
  71.                                   _('Quit onBoard'), self.quit),
  72.                                  ('Settings', gtk.STOCK_PREFERENCES, _('_Settings'), None, _('Show settings'), self.cb_settings_item_clicked)])
  73.  
  74.         uiManager.insert_action_group(actionGroup, 0)
  75.  
  76.         uiManager.add_ui_from_string("""<ui>
  77.                         <popup>
  78.                             <menuitem action="Settings"/>
  79.                             <menuitem action="Quit"/>
  80.                         </popup>
  81.                     </ui>""")
  82.         trayMenu = uiManager.get_widget("/ui/popup")
  83.  
  84.         # Create the trayicon
  85.         self.statusIcon = gtk.status_icon_new_from_file(
  86.                 os.path.join(config.install_dir, "data", "onboard.svg"))
  87.         self.statusIcon.connect("activate", self.cb_status_icon_clicked)
  88.         self.statusIcon.connect("popup-menu", self.cb_status_icon_menu,
  89.                 trayMenu)
  90.  
  91.         _logger.info("Showing window")
  92.         self._window.hidden = False
  93.         self._window.do_show()
  94.         
  95.         config.show_trayicon_notify_add(self.do_set_trayicon)
  96.  
  97.         if config.show_trayicon:
  98.             _logger.info("Showing trayicon")
  99.             self.hide_status_icon()
  100.             self.show_status_icon()
  101.         else:
  102.             self.hide_status_icon()
  103.  
  104.         if main:
  105.             _logger.info("Entering mainloop of onboard")
  106.             gtk.main()
  107.             self.clean()
  108.  
  109.     def cb_settings_item_clicked(self,widget):
  110.         """
  111.         Callback called when setting button clicked in the trayicon menu.
  112.         """
  113.         run_script("sokSettings")
  114.  
  115.     def cb_status_icon_menu(self,status_icon, button, activate_time,trayMenu):
  116.         """
  117.         Callback called when trayicon right clicked.  Produces menu.
  118.         """
  119.         trayMenu.popup(None, None, gtk.status_icon_position_menu,
  120.              button, activate_time, status_icon)
  121.  
  122.     def do_set_trayicon(self, show_trayicon):
  123.         """
  124.         Callback called when gconf detects that the gconf key specifying
  125.         whether the trayicon should be shown or not is changed.
  126.         """
  127.         if show_trayicon:
  128.             self.show_status_icon()
  129.         else:
  130.             self.hide_status_icon()
  131.  
  132.     def show_status_icon(self):
  133.         """
  134.         Shows the status icon.  When it is shown we set a wm hint so that
  135.         onboard does not appear in the taskbar.
  136.         """
  137.         self.statusIcon.set_visible(True)
  138.         self._window.set_property('skip-taskbar-hint', True)
  139.  
  140.     def hide_status_icon(self):
  141.         """
  142.         The opposite of the above.
  143.         """
  144.         self.statusIcon.set_visible(False)
  145.         self._window.set_property('skip-taskbar-hint', False)
  146.  
  147.     def cb_status_icon_clicked(self,widget):
  148.         """
  149.         Callback called when trayicon clicked.
  150.         Toggles whether onboard window visibile or not.
  151.  
  152.         TODO would be nice if appeared to iconify to taskbar
  153.         """
  154.         if self._window.hidden: self._window.do_show()
  155.         else: self._window.do_hide()
  156.  
  157.     def clean(self): #Called when sok is gotten rid off.
  158.         self.keyboard.clean()
  159.         self._window.hide()
  160.  
  161.     def quit(self, widget=None):
  162.         self.clean()
  163.         gtk.main_quit()
  164.             
  165.     def load_layout(self, filename):
  166.         _logger.info("Loading keyboard layout from " + filename)
  167.         self.keyboard = KeyboardSVG(filename)
  168.         self._window.set_keyboard(self.keyboard)
  169.